/*
 * TCSS 422 - Spring quarter 2011
 * Team: 	Googlers
 * Members: Deepa Sahni, Krisnil Gounder, Michael Schweiger
 * Date: 	April 7, 2011
 */
package buffers;

import java.net.URL;
import java.util.HashSet;
import java.util.Set;

/**
 * A buffer designed to hold URL objects.  This buffer will only hold a unique URL once
 * within its lifetime (keeps track of URLs seen).  This object is a Singleton.
 * Null elements are not allowed in this buffer.
 * @author Michael Schweiger
 * @version 1.0
 */
public final class URLBuffer extends Buffer<URL> {

	/**
	 * The singleton instance of this PageBuffer.
	 */
	private static final URLBuffer my_instance = new URLBuffer();
	
	/**
	 * The set of seen URLs.
	 */
	private final Set<URL> my_seen_urls;
		
	/**
	 * Returns a singleton instance of this PageBuffer.
	 * @return The PageBuffer
	 */
	public static URLBuffer getInstance() {
		return my_instance;
	}

	/**
	 * Used privately for instantiation and to prevent outside instantiation.
	 */
	private URLBuffer() {
		super(false);
		my_seen_urls = new HashSet<URL>();
	}
	
	/**
	 * Attempt to add a URL.  Returns true if successfully added.
	 * @throws IllegalArgumentException if url is null.
	 * @param url The URL to add.
	 * @return true if successfully added (not seen before), false otherwise.
	 */
	public synchronized boolean add(URL url) throws IllegalArgumentException {
		if (my_seen_urls.contains(url)) {
			return false;
		} else {
			my_seen_urls.add(url);
			return super.add(url);
		}
	}
	
	/**
	 * Returns the number of elements that have been seen by this buffer.
	 * @return the number of elements that have been seen by this buffer.
	 */
	public synchronized int getSeenCount() {
		return my_seen_urls.size();
	}
}
